package com.goddess.yeecolor.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 报名成功
 */
public class EnrollFinishAcitity extends BaseActivity implements View.OnClickListener{

    private TextView entrofinish_ok,Order_number,Order_jine,Order_detail,Order_other,mTitle;
    private View mView;
    private Toolbar mToobar;
    private String order_id;
    private Bundle bundle;

    private String state;
    private ImageView imagview;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.activity_entrollfinish,null);
        setContentView(mView);
        initData();
        state=getIntent().getStringExtra("tag");
        initFindView();

        initOnClck();
    }
    @Override
    public void initOnClck() {
        Order_other.setOnClickListener(this);
    }

    @Override
    public void initFindView() {

        mToobar= (Toolbar) findViewById(R.id.title_toobar);
        imagview= (ImageView) findViewById(R.id.imagview);
        mTitle= (TextView) findViewById(R.id.title_content);
        entrofinish_ok= (TextView) findViewById(R.id.entrofinish_ok);
        Order_number= (TextView) findViewById(R.id.Order_number);
        Order_jine= (TextView) findViewById(R.id.Order_jine);
//        Order_detail= (TextView) findViewById(R.id.Order_detail);
        Order_other= (TextView) findViewById(R.id.Order_other);

        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view)->{
            if (typed.equals("events")){
                SharePreceUtils.saveOne("type",3,"show");//返回课程
                JumpIntents.startIntent(EnrollFinishAcitity.this, MainActivity.class);
            }
            else if (typed.equals("course")){
                SharePreceUtils.saveOne("type",2,"show");//返回课程
                JumpIntents.startIntent(EnrollFinishAcitity.this, MainActivity.class);
            }

            setFiish(this);
        });
        mTitle.setText("报名结果");

    }
    private dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }

    private Map<Object, Object> map;
    @Override
    public void initData() {
        setProgress();
        map=new HashMap<Object, Object>();
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        map.put("order_id",SharePreceUtils.getOther("order","order_id"));

        OkttpUtils.getIntence().OkttpPost(MyUrl.BUYSHOW,map,handler,1);
    }



    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
//            case R.id.Order_detail:
//
//                break;
            case R.id.Order_other:
                SharePreceUtils.saveOne("type",3,"show");//返回课程
                JumpIntents.startIntent(EnrollFinishAcitity.this, MainActivity.class);
                break;
        }
    }

    private String jsonString,states,typed;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject data=new JSONObject(jsonString);
                JSONObject result=data.getJSONObject("result");

                states=result.optString("state");
                typed=result.optString("typed");
                if (states.equals("1")){
                    entrofinish_ok.setText("恭喜您报名成功");
                    imagview.setImageResource(R.mipmap.applysuccess);
                }
                else if (states.equals("0")){
                    entrofinish_ok.setText("支付失败,请重新支付");
                    imagview.setImageResource(R.mipmap.nonono);
                }
                Order_number.setText(result.optString("order_id"));
                Order_jine.setText(result.optString("price"));
                if (dialog_progress!=null&&dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }
            }

            catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (typed.equals("events")){
            SharePreceUtils.saveOne("type",3,"show");//返回课程
            JumpIntents.startIntent(EnrollFinishAcitity.this, MainActivity.class);
        }
        else if (typed.equals("course")){
            SharePreceUtils.saveOne("type",2,"show");//返回课程
            JumpIntents.startIntent(EnrollFinishAcitity.this, MainActivity.class);
        }
    }
}
